﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using jqwidgetsapi.Models;
using System.Net;


namespace jqwidgetsapi.Controllers
{
    public class SeriesController : ApiController
    {
        static readonly ISeriesRepository repository = new SeriesRepository();
        #region  GET

        // GET /api/Series
        [HttpGet]
        public IQueryable<Series> GetAllSeries()
        {
            return repository.GetAll().AsQueryable(); ;
        }

        // GET /api/Series/id
        [HttpGet]
        public List<DataPoint> GetSeries(short id)
        {
            Series item = repository.Get(id);

            return item.SeriesPoints; //se non esiste ritorna un null
        }

        // GET /api/Series?name=valore
        [HttpGet]
        public IEnumerable<Series> GetSeriesByLabel(string Name)
        {
            return repository.GetAll().Where(
                p => string.Equals(p.Name, Name, StringComparison.OrdinalIgnoreCase));
        }

        #region Paging GET
        [HttpGet]
        public IEnumerable<Series> GetSeries(short pageIndex, short pageSize)
        {
            return repository.GetAll().Skip(pageIndex * pageSize).Take(pageSize);
        }
        #endregion
        #endregion

        #region  POST
        // POST /api/Series
        [HttpPost] //inserisce un Series
        public virtual Series PostSeries(Series item)
        {
            item = repository.Add(item);
            return item;
        }

    

        //[HttpPost] //inserisce un Series
        //public HttpResponseMessage<Series> PostHttpSeries(Series item)
        //{
        //    // Validate movie
        //    if (!ModelState.IsValid)
        //    {
        //        var errors = new JsonArray();
        //        foreach (var prop in ModelState.Values)
        //        {
        //            if (prop.Errors.Any())
        //            {
        //                errors.Add(prop.Errors.First().ErrorMessage);
        //            }
        //        }
        //        //return new HttpResponseMessage<JsonValue>(errors, HttpStatusCode.BadRequest);
        //        return new HttpResponseMessage<Series>(item, HttpStatusCode.BadRequest);
        //    }

        //    item = repository.Add(item);


        //    // Build a response that contains the location of the new movie
        //    var response = new HttpResponseMessage<Series>(item, HttpStatusCode.Created);
        //    var relativePath = "/api/Series/" + item.Id;
        //    response.Headers.Location = new Uri(Request.RequestUri, relativePath);
        //    return response;
        //}
        #endregion


        #region   PUT
        //content-type: application/json
        // PUT /api/Series/id
        [HttpPut]  //modifica
        public void PutSeries(short id, Series item)
        {
            item.Id = id; //se la chiave è l'id non ce ne è bisogno
            if (!repository.Update(item))
            {
                //da gestire
            }
        }
        #endregion

        #region   DELETE
        //// DELETE /api/Series/id
        //[HttpDelete]
        //public void DeleteVoidSeries(short id)
        //{
        //    repository.Remove(id);
        //}

        // DELETE /api/Series/id
        [HttpDelete]
        public Series DeleteSeries(short id)
        {
            Series cal;
            if (!repository.TryGet(id, out cal))
                throw new HttpResponseException(HttpStatusCode.NotFound);
            repository.Delete(id);
            return cal;
        }
        #endregion
    }
}
